package com.lin.filegraph.utils.parser.node;

import com.lin.filegraph.utils.calculator.CodeSizeCalculator;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.CompilationUnit;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: linhuaixu
 * @time: 2023/5/18 10:08
 */

public class JFileNode extends JTreeNode {

    private String fileName = null;
    private String fileType = "java";
    private int validLOC = 0;

    public JFileNode(String filePath, ASTNode astNode, String fileName) {
        super(filePath, astNode);
        this.fileName = fileName;
        if(astNode instanceof CompilationUnit) {
            validLOC = CodeSizeCalculator.calculateValidSizeOfAST((CompilationUnit) astNode);
        }

    }

    public String getFileName() {
        return fileName;
    }

    public int getFileLength() {
        // ��̬���
        return 0;
    }

    public CompilationUnit getCompilationUnit() {
        return (CompilationUnit) getAstNode();
    }

    public String getFileType() {
        return fileType;
    }


    public int getValidLOC() {
        return validLOC;
    }

    public List<JImportNode> getAllImports() {
        List<JImportNode> importNodeList = new ArrayList<JImportNode>();
        List<? extends JTreeNode> tempList = super.getChildrenNodeList();
        for (JTreeNode basedNode : tempList) {
            if (basedNode instanceof JImportNode) {
                JImportNode importNode = (JImportNode) basedNode;
                importNodeList.add(importNode);
            }
        }
        return importNodeList;
    }

    public List<JTypeNode> getAllTypes() {
        List<JTypeNode> typeNodeList = new ArrayList<JTypeNode>();
        List<? extends JTreeNode> tempList = super.getChildrenNodeList();
        for (JTreeNode basedNode : tempList) {
            if (basedNode instanceof JTypeNode) {
                JTypeNode typeNode = (JTypeNode) basedNode;
                typeNodeList.add(typeNode);
            }
        }
        return typeNodeList;
    }

    public List<String> getAllTypeNames() {
        List<String> typeNameList = new ArrayList<String>();
        List<? extends JTreeNode> tempList = super.getChildrenNodeList();
        for (JTreeNode basedNode : tempList) {
            if (basedNode instanceof JTypeNode) {
                JTypeNode typeNode = (JTypeNode) basedNode;
                typeNameList.add(typeNode.getTypeName());
            }
        }
        return typeNameList;
    }

    public List<JClassNode> getAllClasses() {
        List<JClassNode> classNodeList = new ArrayList<JClassNode>();
        List<? extends JTreeNode> tempList = super.getChildrenNodeList();
        for (JTreeNode basedNode : tempList) {
            if (basedNode instanceof JClassNode) {
                JClassNode classNode = (JClassNode) basedNode;
                classNodeList.add(classNode);
            }
        }
        return classNodeList;
    }

    public List<JEnumNode> getAllEnums() {
        List<JEnumNode> enumNodeList = new ArrayList<JEnumNode>();
        List<? extends JTreeNode> tempList = super.getChildrenNodeList();
        for (JTreeNode basedNode : tempList) {
            if (basedNode instanceof JEnumNode) {
                JEnumNode enumNode = (JEnumNode) basedNode;
                enumNodeList.add(enumNode);
            }
        }
        return enumNodeList;
    }

    public List<JInterfaceNode> getAllInterfaces() {
        List<JInterfaceNode> interfaceNodeList = new ArrayList<JInterfaceNode>();
        List<? extends JTreeNode> tempList = super.getChildrenNodeList();
        for (JTreeNode basedNode : tempList) {
            if (basedNode instanceof JInterfaceNode) {
                JInterfaceNode interfaceNode = (JInterfaceNode) basedNode;
                interfaceNodeList.add(interfaceNode);
            }
        }
        return interfaceNodeList;
    }

    public JPackageNode getPackage() {
        JTreeNode fatherNode = super.getFatherNode();
        if (fatherNode instanceof JPackageNode) {
            return (JPackageNode) fatherNode;
        } else {
            System.err.println("ERR");
            return null;
        }
    }

    public String toString() {
        return "[FileNode] " + filePath;
        // " StartLine:" + getStartLine() +
        // " EndLine:" + getEndLine();
    }
}
